{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.9 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "0600588c3b5f4418cbe7b5ebc6825b479f3bc010269d8b60d75058cdd010adfe"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 表达式转换\n",
    "中缀表达式 -> 前缀/后缀\n",
    "\n",
    "*补充一个包的知识*  \n",
    "- 一个代码文件夹里有\\__init\\__.py才叫包\n",
    "- from .XXX import XXX: . 表示当前目录  \n",
    "    ..  表示上一个目录  \n",
    "    ... 表示上两级，以此类推  "
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from stack import Stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# infix     : 中缀\n",
    "# postfix   : 后缀\n",
    "# priority  : 优先级\n",
    "# expression: 表达式\n",
    "def infixToPostfix(infixexpr):\n",
    "    \"\"\"中缀到后缀\"\"\"\n",
    "\n",
    "    # 记录操作符优先级\n",
    "    prec = {}\n",
    "    prec[\"*\"] = 3\n",
    "    prec[\"/\"] = 3\n",
    "    prec[\"+\"] = 2\n",
    "    prec[\"-\"] = 2\n",
    "    prec[\"(\"] = 1\n",
    "\n",
    "    # 操作数列表\n",
    "    opStack = Stack()\n",
    "    # 后缀列表\n",
    "    postfixList = []\n",
    "    # 解析表达式到单词列表\n",
    "    tokenList = infixexpr.split(' ')\n",
    "    # print(\"tokenList:\", tokenList)\n",
    "\n",
    "    # 扫描列表\n",
    "    for token in tokenList:\n",
    "        # 如果是操作数就添加到列表\n",
    "        if token in \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\" or token in \"0123456789\":\n",
    "            postfixList.append(token)\n",
    "        # 如果是左括号就压栈\n",
    "        elif token == '(':\n",
    "            opStack.push(token)\n",
    "        # 如果是右括号，就把括号里的运算符pop出来\n",
    "        # 一直pop到遇见上一个左括号\n",
    "        elif token == ')':\n",
    "            topToken = opStack.pop()\n",
    "            while topToken != '(':\n",
    "                postfixList.append(topToken)\n",
    "                topToken = opStack.pop()\n",
    "        # 普通的运算符\n",
    "        else:\n",
    "            # 如果栈顶的运算符优先级更高，就pop出来\n",
    "            while (not opStack.isEmpty()) and \\\n",
    "                (prec[opStack.peek()] >= prec[token]):\n",
    "                postfixList.append(opStack.pop())\n",
    "            # 把当前运算符压入栈\n",
    "            opStack.push(token)\n",
    "\n",
    "    # 把剩余的运算符pop出来\n",
    "    while not opStack.isEmpty():\n",
    "        postfixList.append(opStack.pop())\n",
    "\n",
    "    # 用空格隔开，并返回\n",
    "    return \" \".join(postfixList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "A B * 3 + 5 3 C + * -\n"
     ]
    }
   ],
   "source": [
    "infixexpr = \"A * B + 3 - 5 * ( 3 + C )\"\n",
    "postfixexpr = infixToPostfix(infixexpr=infixexpr)\n",
    "print(postfixexpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "1 2 * 3 4 / + 5 2 * + 3 -\n"
     ]
    }
   ],
   "source": [
    "test = \"1 * 2 + 3 / 4 + ( 5 * 2 ) - 3\"\n",
    "postfixexpr2 = infixToPostfix(test)\n",
    "print(postfixexpr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "9.75"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "1 * 2 + 3 / 4 + (5 * 2) - 3"
   ]
  }
 ]
}